package com.czh.service.User;

import com.czh.dao.BaseDao;
import com.czh.dao.User.UserDao;
import com.czh.dao.User.UserDaoImpl;
import com.czh.pojo.User;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService{
    private UserDao userDao = null;
//    public UserServiceImpl(UserDao userDao) {
//        this.userDao = userDao;
//    }
    public UserServiceImpl() {
        this.userDao = new UserDaoImpl();//实例化的Dao层保存复用
    }
    @Override
    public User userLogin(String userCode, String userPassword) {
        Connection connection;
        User loginUser;
        try {
            connection = BaseDao.getConnection();
            loginUser = userDao.getLoginUser(connection, userCode,userPassword);
            BaseDao.closeConnect(connection,null,null);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return loginUser;
    }

    @Override
    public boolean UpdatePsd(int id, String userPassword) {
        Connection connection;
        boolean flag = false;
        try {
             connection= BaseDao.getConnection();
            int i = userDao.updatePassword(connection, id, userPassword);
            if(i>=1){
                flag = true;
            }
            BaseDao.closeConnect(connection,null,null);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return flag;
    }

    @Override
    public int userCount(String username, int userRole) {
        Connection connection;
        int count;
        try {
            connection = BaseDao.getConnection();
            count= userDao.userCount(connection, username, userRole);
            BaseDao.closeConnect(connection,null,null);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count;
    }

    @Override//用户列表，分页
    public List<User> userList(String username, int userRole, int currentPageNo, int pageSize) {
        Connection con;
        List<User> userList;
        try {
            con= BaseDao.getConnection();
            userList = userDao.getUserList(con, username, userRole, currentPageNo, pageSize);
            BaseDao.closeConnect(con,null,null);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return userList;
    }

    @Override
    public boolean userAdd(User user) throws SQLException {
        Connection connection = null;
        int addRows ;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);//开启JDBC事务。
            addRows = userDao.userAdd(connection,user);
            connection.commit();//一定要在Service层关闭连接,否则在DAO层关闭以后事务无法提交
            if (addRows>0){
                flag = true;
            }
        } catch (Exception e) {
            try {
                if (connection != null) {
                    connection.rollback();
                }
                System.out.println("========rollback=========");
            } catch (SQLException c) {
                c.printStackTrace();
            }
        } finally {
            //在service层进行connection连接的关闭.
            BaseDao.closeConnect(connection,null,null);
        }
        return flag;
    }
    @Override
    public User userCodeSelect(String userCode) {
        Connection con;
        User user = null;
        try {
            con = BaseDao.getConnection();
            if(con!=null){
                user = userDao.userCodeSeek(con, userCode);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return user;
    }

    @Override
    public User userIdSelect(Integer id) {
        User user;
        try {
            Connection con = BaseDao.getConnection();
            user = userDao.userIdSeek(con, id);
            BaseDao.closeConnect(con,null, null);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return user;
    }

    @Override
    public boolean userDelete_id(Integer id) throws SQLException {
        boolean flag = false;
        Connection con = null;
        try {
            con= BaseDao.getConnection();
            con.setAutoCommit(false);
            if ( userDao.userDelete_id(con, id)>0){
                flag = true;
                con.commit();
            }
        }catch (Exception e){
            try {
                if (con != null) {
                    con.rollback();
                }
            } catch (SQLException ex) {
                throw new RuntimeException("事务回滚失败!");
            }
        }finally {
                BaseDao.closeConnect(con,null,null);
        }
        return flag;
    }

    @Override
    public boolean modify(User user) {
        Connection con = null;
        boolean flag = false;
        try {
            con = BaseDao.getConnection();
            con.setAutoCommit(false);
            if(userDao.modify(con, user)>0){
                flag = true;
                con.commit();
            }
        } catch (SQLException e) {
            try {
                if (con != null) {
                    con.rollback();
                }
            } catch (SQLException ex) {
                throw new RuntimeException("回滚失败!");
            }
        }
        return flag;
    }
}
